package com.qf.homework.service.impl;

import cn.hutool.jwt.JWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.homework.controller.Constans;
import com.qf.homework.dao.TicketMapper;
import com.qf.homework.entity.Ticket;
import com.qf.homework.entity.Vote;
import com.qf.homework.service.TicketService;
import com.qf.homework.service.VoteService;
import com.qf.homework.vo.ResultVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.sun.javaws.JnlpxArgs.verify;

@Service
public class TicketServiceImpl extends ServiceImpl<TicketMapper, Ticket> implements TicketService {
    //存放手机号和对应的token信息
    private static final Map<String, String> map =new HashMap<>();
    //存放手机号和对应的最后一次投票时间
    private static final Map<String, Date> voteMap =new HashMap<>();


    @Resource
    VoteService voteService;

    @Override
    public ResultVo getToken(String phone)   {
        String token=null;
        if (map.get(phone)==null) {
            token=JWT.create().setPayload(phone, System.currentTimeMillis())
                    .setExpiresAt(new Date(System.currentTimeMillis() + 1000 * 60))
                    .setKey("toupiao".getBytes(StandardCharsets.UTF_8))
                    .sign();
            map.put(phone,token);
            Vote vote = new Vote();
            vote.setPhoneNumber(phone);
            vote.setIsVote("0");
            voteService.save(vote);
            return ResultVo.success(token);
        }
        String s = map.get(phone);
        long timeStamp =(long) JWT.of(s).getPayload(phone);
        if (System.currentTimeMillis()-timeStamp<=1000*60){
            return ResultVo.fail(map.get(phone));
        }
        else {
            token=JWT.create().setPayload(phone, System.currentTimeMillis())
                    .setExpiresAt(new Date(System.currentTimeMillis() + 1000 * 60))
                    .setKey("toupiao".getBytes(StandardCharsets.UTF_8))
                    .sign();
            map.put(phone,token);
            Vote vote1 = new Vote();
            vote1.setPhoneNumber(phone);
            vote1.setIsVote("0");
            voteService.save(vote1);
            return ResultVo.success(token);
        }

    }

    @Override
    public ResultVo vote(String starName, String phone, String token1)   {

        Date date = voteMap.get(phone);

        if (date!=null){
           if (System.currentTimeMillis()-date.getTime()<60*1000){
               return ResultVo.fail("投票间隔过短");
           }
        }
        List<Vote> list = voteService.list(new QueryWrapper<Vote>()
                .eq("phone_number", phone)
                .eq("is_vote", "0"));
        if (list==null|| list.size()==0){
            return ResultVo.fail("投票失败,请重新获取票数");
        }else {
            boolean verify = JWT.of(token1).setKey("toupiao".getBytes(StandardCharsets.UTF_8)).verify();
            if (verify){
                Vote vote = list.get(list.size() - 1);
                vote.setIsVote("1");
                vote.setTimeStamp(new Date());
                vote.setStar(starName);
                voteService.updateById(vote);
                voteMap.put(phone,vote.getTimeStamp());
                Ticket name = getOne(new QueryWrapper<Ticket>()
                        .eq("name", starName));
                name.setNums(name.getNums()+1);
                updateById(name);
                return ResultVo.success();
            }else {
                return ResultVo.fail("投票失败,请重新获取票数");
            }
        }

    }


}
